/*-----------------------------------------------------------------------------------
 *
 * 프로그램명	: HomePageDAO.java
 * Version		: 1.0
 * 작성일 		: 2006-08-16
 * 작성자		:
 * 수정일 		:
 * 수정자		:
 * 설명		 	:
 * 기능		 	:
 *
 * Copyright (c) 2000 by H&H  All Rights Reserved.
 *
-------------------------------------------------------------------------------------*/
package com.qbooks.cms.admin;

import java.io.*;

import java.util.*;
import java.sql.*;

import com.zll.framework.db.DBManager;
import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;



public class AdminDAO
{
	public HashList selectCodeList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/admin.xml", "select_code_list", infos);
	}
	
	public HashList selectUserList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/admin.xml", "select_user_list", infos);
	}

	public HashList selectPublisherList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/admin.xml", "select_publisher_list", infos);
	}
	
	public HashList selectAppList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/admin.xml", "select_app_list", infos);
	}
	
	public HashList selectAppHistoryList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/admin.xml", "select_app_history_list", infos);
	}
	/**
	 * User 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveUser(HashBox box) throws Exception
    {
		Connection conn = null;

        try
		{
			conn = DBManager.getConnection();

			ArrayList dataList = (ArrayList)box.getList("list");

		    for (int i = 0; i < dataList.size(); i++)
		    {
				HashMap map = (HashMap)dataList.get(i);

				if ("C".equals(map.get("job_type"))) {
		    		DBManager.executeUpdate("/admin.xml", "insert_user", map, conn);
		       	} else if ("U".equals(map.get("job_type"))) {
		       		DBManager.executeUpdate("/admin.xml", "update_user", map, conn);
		       	} else if ("D".equals(map.get("job_type"))) {
		       		DBManager.executeUpdate("/admin.xml", "delete_user", map, conn);
		        }
			}

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }


	/**
	 * Code 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveCode(HashBox box) throws Exception
    {
		Connection conn = null;
        //boolean result	= true;

        try
		{
			conn = DBManager.getConnection();


			DBManager.executeUpdate("/admin.xml", "delete_all_code", box.getParam(), conn);

			ArrayList dataList = (ArrayList)box.getList("list");

		    for (int i = 0; i < dataList.size(); i++)
		    {
				HashMap map = (HashMap)dataList.get(i);

		    	DBManager.executeUpdate("/admin.xml", "insert_code", map, conn);
			}


			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }


	/**
	 * 출판사 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void savePublisher(HashBox box) throws Exception
    {
		Connection conn = null;

        try
		{
			conn = DBManager.getConnection();

			HashMap map = box.getParam();

			if ("C".equals(map.get("job_type"))) {
	    		DBManager.executeUpdate("/admin.xml", "insert_publisher", map, conn);
	       	} else if ("U".equals(map.get("job_type"))) {
	       		DBManager.executeUpdate("/admin.xml", "update_publisher", map, conn);
	       	} else if ("D".equals(map.get("job_type"))) {
	       		DBManager.executeUpdate("/admin.xml", "delete_publisher", map, conn);
	        }

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }
	
	/**
	 * 앱 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveApp(HashBox box) throws Exception
    {
		Connection conn = null;

        try
		{
			conn = DBManager.getConnection();

			HashMap map = box.getParam();

			if ("C".equals(map.get("job_type"))) {
	    		DBManager.executeUpdate("/admin.xml", "insert_app", map, conn);
	       	} else if ("U".equals(map.get("job_type"))) {
	       		DBManager.executeUpdate("/admin.xml", "update_app", map, conn);
	       	} else if ("D".equals(map.get("job_type"))) {
	       		DBManager.executeUpdate("/admin.xml", "delete_app", map, conn);
	        }

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
    }
	
	/**
	 * 앱 업데이트 이력 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public void saveAppHistory(HashBox box) throws Exception
   {
		Connection conn = null;

       try
		{
			conn = DBManager.getConnection();

			HashMap map = box.getParam();

			if ("C".equals(map.get("job_type"))) {
	    		DBManager.executeUpdate("/admin.xml", "insert_app_history", map, conn);
	       	} else if ("U".equals(map.get("job_type"))) {
	       		DBManager.executeUpdate("/admin.xml", "update_app_history", map, conn);
	       	} else if ("D".equals(map.get("job_type"))) {
	       		DBManager.executeUpdate("/admin.xml", "delete_app_history", map, conn);
	        }

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	DBManager.rollback(conn);
	    	throw e;
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}
   }
}
